package io.github.ali.commons.validation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.PrioritizedParameterNameDiscoverer;
import org.springframework.core.annotation.Order;

import java.util.Map;

/**
 * @author bailey
 */
@Configuration
public class ValidationAspectConfiguration {
	@Bean
	@Order(1)
	public ValidationAspect aliValidationAspect(ApplicationContext applicationContext) {
		ValidateRender validateRender = null;
		try {
			validateRender = applicationContext.getBean(ValidateRender.class);
		} catch (Exception e) {
			validateRender = new ValidateRender() {};
		}
		Map<String, PrioritizedParameterNameDiscoverer> existed = applicationContext.getBeansOfType(PrioritizedParameterNameDiscoverer.class);
		return new ValidationAspect(validateRender, existed.size() == 0 ? new DefaultParameterNameDiscoverer() : existed.get(0));
	}
}
